const api = require('../../utils/api.js')
import { $wuxToast } from '../../dist/index';
var integral;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    options: [],
    value3: [],
    displayValue3: '请选择',
    name:'',
    content:'',
    integral:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getAllType()
  },
  inputPhone(e){
    this.setData({
      name: e.detail.value,
    })
  },
  inputIntroduction(e){
    this.setData({
      content: e.detail.value,
    })
  },
  inputIntegral(e){ 
    this.setData({
      integral: e.detail.value,
    })
  },
  
  getAllType(){
    var that = this
    wx.request({
      url: api.getAllType,
      success(res){
        var typeList = res.data.data
        var type = {}
        var types = []
        for (let i = 0; i < typeList.length; i++) {
          type.value = typeList[i].id
          type.label = typeList[i].name
          types.push(type)
          type={}
        }
        that.setData({
          options: types
        })
      }
    })
  },
  setValue(values, key) {
    this.setData({
        [`value${key}`]: values.value,
        [`displayValue${key}`]: values.label,
    })
  },
  onConfirm(e) {
      const { index } = e.currentTarget.dataset
      this.setValue(e.detail, index)
      console.log(`onConfirm${index}`, e.detail)
  },
  
  addQuickQuestionInfo(e){//紧急发布 
    var type = this.data.value3[0]
    var content = this.data.content
    var name = this.data.name
      // integral = this.date.integral
    console.log(this.data.integral)
    var that = this
    if(name==''||content==''||integral==''){
      that.showToastCancel("字段不能为空")
    } else {
      wx.getStorage({
        key: 'userId',
        success(res){
          wx.request({
            url: api.addUrgencyQuestion,
            method:"POST",
            data:{
              type:type,
              content:content,
              title:name,
              userId: res.data,
              integral:that.data.integral
            },
            success(res){
              if(res.data.errno == 0){
                that.showToast("添加成功")
              }
              that.setData({
                value3:[],
                content:''
              })
            }
          })
        }
      })
    }
  },
  addQuestionInfo(e){
    var type = this.data.value3[0]
    var content = this.data.content
    var name = this.data.name
    var that = this
    if(name==''||content==''){
      that.showToastCancel("字段不能为空")
    } else {
      wx.getStorage({
        key: 'userId',
        success(res){
          wx.request({
            url: api.addQuestion,
            method:"POST",
            data:{
              type:type,
              content:content,
              title:name,
              userId: res.data
            },
            success(res){
              if(res.data.errno == 0){
                that.showToast("添加成功")
              }
            }
          })
        }
      })
    }
  },
  showToast(content) {
    $wuxToast().show({
      type: 'success',
      duration: 1500,
      color: '#fff',
      text: content,
      success: () => {}
    })
  },
  showToastCancel(content) {
    $wuxToast().show({
        type: 'cancel',
        duration: 1500,
        color: '#fff',
        text: content,
        success: () => {}
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})